package model;

import helper.Helper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class PacienteFactory {
	private final String ARQUIVO = "pacientes.txt";
	private File file;
	private FileWriter fw;
	private BufferedWriter bw;
	private FileReader fr;
	private BufferedReader br;
	private String path;

	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	
	public boolean gravarPaciente(PacienteBean paciente){
		
		file = new File(getPath() + "//" + ARQUIVO );
		try{
		fw = new FileWriter(file, true);
		bw = new BufferedWriter(fw);
		StringBuilder conteudo = new StringBuilder();
		conteudo.append(paciente.getCodigo());
		conteudo.append(",").append(paciente.getNome());
		conteudo.append(",").append(paciente.getMotivo());
		conteudo.append(",").append(paciente.getSexo());
		conteudo.append(",").append(paciente.getTipoSanguineo());
		conteudo.append(",").append(Helper.calendarToString(paciente.getDataNascimento(), "dd/MM/yyyy"));
		conteudo.append("\r");
		bw.write(conteudo.toString());
		bw.close();
		return true;
		}catch (Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}
		public ArrayList<PacienteBean> carregarArquivo(){
			file = new File(getPath() + "//" + ARQUIVO);
			if(file.exists()){
				return null;
			}
			try {
				
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			String linha = "";
			while((linha = br.readLine())!=null){
				Object itens[] = linha.split(",");
				System.out.println();
			}
			}catch (Exception e){
				System.out.println(e.getMessage());
				return null;
			}
			return null;
		}
	}
